//
//  LCHomeController.swift
//  SinaWeiBo
//
//  Created by mac on 16/11/30.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import YYModel

class LCHomeController: LCVisitorBaseController {
    //  对应的视图模型(ViewModel)
    fileprivate lazy var homeStatusViewModel = LCHomeStatusesViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if isLogon == false {
            visitorView?.updateVisitorView(imageName: nil, message: nil)
        }else{
    
        setupUI()
        loadHomeData()
        }

    }
    //懒加载一个控件
    fileprivate lazy var activityView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.red
        return view
    }()
    
    //懒加载一个下拉刷新控件
    fileprivate lazy var pullDownView:LCRefeshControl = {
       let refesh = LCRefeshControl()
        refesh.addTarget(self, action: #selector(refreshAction), for: .valueChanged)
        return refesh
    }()
    
    //懒加载一个toplabel
    private lazy var topLabel:UILabel = {
       let label = UILabel()
        label.text = "没有最新的数据加载"
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.white
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    
    private func setupUI(){

        
        if let nav = self.navigationController{
            //设置toplabel的大小
            topLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35 , width: ScreenWidth, height: 35)
            nav.view.insertSubview(topLabel, belowSubview: nav.navigationBar)
        }
        setupTableView()
    }
    
    private func startAnimation(count: String){
        
        if !topLabel.isHidden {
            return
        }
        
        topLabel.isHidden = false
        topLabel.text = count
        
        UIView.animate(withDuration: 1, animations: {
            self.topLabel.transform = CGAffineTransform(translationX: 0, y: 35)
        }, completion: {(_) -> Void in
            
            UIView.animate(withDuration: 1, animations: {
            self.topLabel.transform = CGAffineTransform.identity
            
            }, completion: { (_) in
                self.topLabel.isHidden = true
                
            })
        
        
        })
    
    }
    
    @objc private func refreshAction(){
        print("加载数据")
        loadHomeData()
    }
    
    private func setupTableView(){
        //注册cell
        tableView.register(LCHomeStatusesCell.self, forCellReuseIdentifier: cellIdentifier)
        //设置tableview的行高
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        //  去掉分割线
        tableView.separatorStyle = .none
        
        //上拉加载
        tableView.tableFooterView = activityView
        
        //自定义下拉刷新
        tableView.addSubview(pullDownView)
        
        
        
    }
    
    //加载主页数据
    func loadHomeData(){
        homeStatusViewModel.loadStatuses(isUp: activityView.isAnimating) {(isSuccess,count) in
            //数据加载完成以后停止菊花转动
            if !self.activityView.isAnimating{
                self.startAnimation(count: count)
            }
            self.activityView.stopAnimating()
            self.pullDownView.endRefesh()
            
            if isSuccess{
                //如果加载成功就刷新tableview界面
                self.tableView.reloadData()
            }
        }
    }
    
    
}
//MARK:tableviewDataSource数据源方法
extension LCHomeController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeStatusViewModel.modelList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LCHomeStatusesCell
        //设置cell的选中状态的样式
        cell.selectionStyle = .none
        
        let model = homeStatusViewModel.modelList[indexPath.row]
        cell.cellViewModel = model
    
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == homeStatusViewModel.modelList.count - 1 && !activityView.isAnimating{
            print("到最后一个cell了")
            activityView.startAnimating()
            //加载数据
            loadHomeData()
        }
    }
    
}






